package com.mdh.android.vm.av;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

    int mGalleryItemBackground;
    private Context mContext;
    private Integer[] mImageIds = null;
    private Bitmap[] mBmps = null;
    private ImageView[] mImages;

    public ImageAdapter(Context c, Integer[] ImageIds) {
	mContext = c;
	mImageIds = ImageIds;
	mImages = new ImageView[mImageIds.length];
    }

    public ImageAdapter(Context c, Bitmap[] Bmps) {
	mContext = c;
	mBmps = Bmps;
	mImages = new ImageView[Bmps.length];
    }

    public boolean createReflectedImages() {
	int index = 0;

	if (mBmps != null) {
	    for (int i = 0; i < mBmps.length; i++) {
		Bitmap originalImage = mBmps[i];
		int width = 100;
		int height = 100;
		if (originalImage != null) {
		    width = originalImage.getWidth();
		    height = originalImage.getHeight();
		}

		Matrix matrix = new Matrix();
		matrix.preScale(1, -1);

		Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height), Config.ARGB_8888);

		if (originalImage != null) {
		    Canvas canvas = new Canvas(bitmapWithReflection);
		    canvas.drawBitmap(originalImage, 0, 0, null);
		}

		ImageView imageView = new ImageView(mContext);
		imageView.setImageBitmap(bitmapWithReflection);
		imageView.setLayoutParams(new GalleryFlow.LayoutParams(120, 100));
		// imageView.setScaleType(ScaleType.MATRIX);
		mImages[index++] = imageView;
	    }
	    return true;
	}

	for (int imageId : mImageIds) {
	    Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
	    int width = originalImage.getWidth();
	    int height = originalImage.getHeight();

	    Matrix matrix = new Matrix();
	    matrix.preScale(1, -1);

	    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height), Config.ARGB_8888);

	    Canvas canvas = new Canvas(bitmapWithReflection);

	    canvas.drawBitmap(originalImage, 0, 0, null);

	    ImageView imageView = new ImageView(mContext);
	    imageView.setImageBitmap(bitmapWithReflection);
	    imageView.setLayoutParams(new GalleryFlow.LayoutParams(120, 100));
	    // imageView.setScaleType(ScaleType.MATRIX);
	    mImages[index++] = imageView;
	}
	return true;
    }

    @Override
    public int getCount() {
	if (mBmps != null)
	    return mBmps.length;

	return mImageIds.length;
	// return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int position) {
	return position;
    }

    @Override
    public long getItemId(int position) {
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	return mImages[position];
	// return mImages[position % mImages.length];
    }

    public float getScale(boolean focused, int offset) {
	return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
    }

}
